$(document).ready(function() {
	// 会员编辑确认
	$("#memberOK").click(function() {
		
		if ($("#imagePath_member").val() == "") {
			alertError("头像不能为空！");
			return false;
		}
		if ($("#fingerImg_member").attr("src") != base + "commonCss/images/zhiwen_c.png") {
			alertError("指纹必须采集！");
			return false;
		}
		if ($("#memName_member").val() == "") {
			alertError("姓名不能为空！");
			return false;
		}
		if ($("#girlRadioButton_member")[0].checked == false && $("#boyRadioButton_member")[0].checked == false) {
			alertError("性别不能为空！");
			return false;
		}
		if ($("#birthday_member").val() == "") {
			alertError("生日不能为空！");
			return false;
		}
		if ($("#country_member").val() == "") {
			alertError("国籍不能为空！");
			return false;
		}
		if (($("#pwd1_member").val() != "" || $("#pwd2_member").val() != "") && $("#pwd1_member").val() != $("#pwd2_member").val() != "") {
			alertError("接领密码必须相同！");
			return false;
		}
		if ($("#fkSales_member").val() == "") {
			alertError("销售不能为空！");
			return false;
		}
		$.messager.confirm('提示', '确认要保存吗?', function(r) {
			if (r) {

				$("#fing1_member").val($("#fing1").val());
				$("#fing2_member").val($("#fing2").val());
				$("#ftype1_member").val($("#ftype1").val());
				$("#ftype2_member").val($("#ftype2").val());
				
				$.ajax({
					cache : false,
					type : "post",
					dataType : "json",
					url : base + "filemanagement/saveMember.shtml",
					data : $("#memberForm").serialize(),
					success : function(data) {
						if (data != null) {
							
							if (data.result == "1") {
								if (data.familyId != null) {
									// 首次添加会员，生成新的familyId
									familyId = data.familyId;
									$("#fkMemId_gua").val(data.memberId);
								}
								//alertInfo("保存成功");
								$('#memberPop').window('close');
								var hisType="${hisType}";
								var memType = $("#memType").val();
								window.location.href = base + "filemanagement/getVipUserDetail.shtml?memberId=" + data.memberId + "&type=" + memType + "&hisType="+hisType;
							} else if (data.result == "0") {
								alertError(data.message);
							}
						} else {
							alertError("保存失败！");
						}
					},
					error : function(XMLHttpRequest) {
						ajaxError(XMLHttpRequest);
					}
				});

			}
		});
	});

	// 非会员编辑确认
	$("#notMemberOK").click(function() {

		if ($("#imagePath_notMember").val() == "") {
			alertError("头像不能为空！");
			return false;
		}
		if ($("#fingerImg_notMember").attr("src") != base + "commonCss/images/zhiwen_c.png") {
			alertError("指纹必须采集！");
			return false;
		}
		if ($("#memName").val() == "") {
			alertError("姓名不能为空！");
			return false;
		}
		if ($("#girlRadioButton")[0].checked == false && $("#boyRadioButton")[0].checked == false) {
			alertError("性别不能为空！");
			return false;
		}
		if ($("#birthday").val() == "") {
			alertError("生日不能为空！");
			return false;
		}
		if ($("#country").val() == "") {
			alertError("国籍不能为空！");
			return false;
		}
		if (($("#pwd1").val() != "" || $("#pwd2").val() != "") && $("#pwd1").val() != $("#pwd2").val() != "") {
			alertError("接领密码必须相同！");
			return false;
		}
		if ($("#memType").val() == "3" && $("#fkSales").val() == "") {
			alertError("销售不能为空！");
			return false;
		}

		$.messager.confirm('提示', '确认要保存吗?', function(r) {
			if (r) {
				$("#fing1_notMember").val($("#fing1").val());
				$("#fing2_notMember").val($("#fing2").val());
				$("#ftype1_notMember").val($("#ftype1").val());
				$("#ftype2_notMember").val($("#ftype2").val());
				$.ajax({
					cache : false,
					type : "post",
					dataType : "json",
					url : base + "filemanagement/saveMember.shtml",
					data : $("#notMemberForm").serialize(),
					success : function(data) {
						if (data != null) {
							
							if (data.result == "1") {
								if (data.familyId != null) {
									// 首次添加会员，生成新的familyId
									familyId = data.familyId;
									$("#fkMemId_gua").val(data.memberId);
								}
								//alertInfo("保存成功");
								$('#notMemberPop').window('close');
								var hisType="${hisType}";
								var memType = $("#memType").val();
								window.location.href = base + "filemanagement/getVipUserDetail.shtml?memberId=" + data.memberId + "&type=" + memType + "&hisType="+hisType;
							} else if (data.result == "0") {
								alertError(data.message);
							}
						} else {
							alertError("保存失败！");
						}
					},
					error : function(XMLHttpRequest) {
						ajaxError(XMLHttpRequest);
					}
				});
			}
		});
	});

	// 监护人编辑确认
	$("#guaOK").click(function() {
		if ($("#imagePath_gua").val() == "") {
			alertError("头像不能为空！");
			return false;
		}
		if ($("#name").val() == "") {
			alertError("姓名不能为空！");
			return false;
		}
		if ($("#telNum").val() == "") {
			alertError("电话不能为空！");
			return false;
		}
		if (!/\d+/.test($("#telNum").val())) {
			alertError("电话只能为数字！");
			return false;
		}

		$.messager.confirm('提示','确认要保存吗?',function(r) {
			if (r) {
				$("#fing1_gua").val($("#fing1").val());
				$("#fing2_gua").val($("#fing2").val());
				$("#ftype1_gua").val($("#ftype1").val());
				$("#ftype2_gua").val($("#ftype2").val());
				$.ajax({
					cache : false,
					type : "post",
					dataType : "json",
					url : base + "filemanagement/saveGua.shtml?country=" + $("#country_gua").val(),
					data : $("#guaForm").serialize(),
					success : function(data) {
						if (data != null) {
							if (data.result == "1") {
								alertInfo("保存成功");
								$('#notMemberPop').window('close');
								window.location.reload();
							} else if (data.result == "0") {
								alertError(data.message);
							}
						} else {
							alertError("保存失败！");
						}
					},
					error : function(XMLHttpRequest) {
						ajaxError(XMLHttpRequest);
					}
				});

			}
		});
});
	
	
	//上传合同【确认】
	/*$("#uploadFeteOK").click(function() {
		if ($("#feteFile").val() == "") {
			alertError("请选择文件！");
			return false;
		}
	$.messager.confirm('提示','确定上传吗',function(r) {
	if (r) {
		$.ajaxFileUpload({
			url: base + 'filemanagement/uploadFete.shtml',
			secureuri:false,                       //是否启用安全提交,默认为false 
			fileElementId:'feteFile',           //文件选择框的id属性
			dataType:'text',                       //服务器返回的格式,可以是json或xml等
			success:function(data){        //服务器响应成功时的处理函数
				if (data != "") {
					//alertInfo("上传成功!");
					$("#fkContract").val(data);
					var memberId=$("#memberId").val();
					updateHisMember(memberId,'1');//执行
				} else {
					alertErrro('上传失败，请重试！');
				}
			},
			error: function(XMLHttpRequest) {
			 ajaxError(XMLHttpRequest);
			}
		});
	}
	});
	});*/
	
	//上传声明【确认】
	/*$("#uploadFeteOK2").click(function() {
		if ($("#feteFile2").val() == "") {
			alertError("请选择文件！");
			return false;
		}
	$.messager.confirm('提示','确定上传吗',function(r) {
	if (r) {
		$.ajaxFileUpload({
			url: base + 'filemanagement/uploadFete.shtml',
			secureuri:false,                       //是否启用安全提交,默认为false 
			fileElementId:'feteFile2',           //文件选择框的id属性
			dataType:'text',                       //服务器返回的格式,可以是json或xml等
			success:function(data){        //服务器响应成功时的处理函数
				if (data != "") {
					//alertInfo("上传成功!");
					$("#fkPlayDiscla").val(data);
					var memberId=$("#memberId").val();
					updateHisMember(memberId,'2');//执行
				} else {
					alertErrro('上传失败，请重试！');
				}
			},
			error: function(XMLHttpRequest) {
			 ajaxError(XMLHttpRequest);
			}
		});
	}
	});
	});*/
	
	//上传停卡【确认】
	/*$("#uploadFeteOK3").click(function() {
		if ($("#feteFile3").val() == "") {
			alertError("请选择文件！");
			return false;
		}
		$.ajaxFileUpload({
			url: base + 'filemanagement/uploadFete.shtml',
			secureuri:false,                       //是否启用安全提交,默认为false 
			fileElementId:'feteFile3',           //文件选择框的id属性
			dataType:'text',                       //服务器返回的格式,可以是json或xml等
			success:function(data){        //服务器响应成功时的处理函数
				if (data != "") {
					alertInfo("上传成功!");
					$("#fkFeteFile").val(data);
					$("#mngFile").val(data);
				} else {
					alertErrro('上传失败，请重试！');
				}
			},
			error: function(XMLHttpRequest) {
			 ajaxError(XMLHttpRequest);
			}
		});
	});*/
	
});

//打开编辑会员
function openMember(memberId) {
	$("#imagePath_member").val();
	$("#fing1_member").val("");
	$("#fing2_member").val("");
	if (memberId == '') {
		$("#pkMemId").val("");
		$("#memberImage").attr("src", base + "commonCss/images/index_03.gif");
		$("#memName_member").val("");
		$("#memberCardName_member").val("");
		$("#girlRadioButton_member").attr("checked", false);
		$("#boyRadioButton_member").attr("checked", false);
		$("#isVip_member").attr("checked", false);
		$("#birthday_member").val("");
		$("#country_member").val("");
		$("#pk_member").hide();
		$("#spanMemberId_member").html("");
		$("#canConsume_member").attr("checked", false);
		$("#pwd1_member").val("");
		$("#pwd2_member").val("");
		$("#signDisclaimer").attr("checked", false);
		$("#note").text("");
		$("#memo").text("");
		$("#fkSales_member").val("");
		$("#fing1").val("");
		$("#fing2").val("");
		$("#fingerImg_member").attr("src", base + "commonCss/images/zhiwen_w.png");
		refreshSelect();
		$("#memberPop").attr("title", "新增");
		$('#memberPop').window('open');
	} else {
		$.ajax({
			cache : false,
			type : "get",
			dataType : "json",
			url : base + "filemanagement/queryMemberById.shtml",
			data : "memberId=" + memberId,
			success : function(data) {
				if (data == null) {
					alertInfo("没有匹配的会员!");
				} else {
					$("#pkMemId_member").val(data.pkMemId);
					if (data.imagePath == null) {
						$("#memberImage").attr("src",base + "commonCss/images/index_03.gif");
					} else {
						$("#imagePath_member").val(data.imagePath);
						$("#memberImage").attr("src",base + "common/showImage.shtml?imagePath=" + data.imagePath);
					}
					$("#memName_member").val(data.memName);
					$("#memberCardName").html(data.memberCardName);
					if (data.sex == "男") {
						$("#boyRadioButton_member").attr("checked", true);
					} else {
						$("#girlRadioButton_member").attr("checked", true);
					}
					if (data.isVip == "1") {
						$("#isVip_member").attr("checked", true);
					} else {
						$("#isVip_member").attr("checked", false);
					}
					$("#birthday_member").val(data.birthdayPage.replace(/\./g, "-"));
					$("#country_member").val(data.country);
					$("#spanMemberId_member").html(data.pkMemId);
					if (data.canConsume == "1") {
						$("#canConsume_member").attr("checked", true);
					} else {
						$("#canConsume_member").attr("checked", false);
					}
					$("#pwd1_member").val(data.receivePwd);
					$("#pwd2_member").val(data.receivePwd);
					if (data.signDisclaimer == "1") {
						$("#signDisclaimer_member").attr("checked", true);
					} else {
						$("#signDisclaimer_member").attr("checked", false);
					}
					var note="";
					if(data.note!=null && data.note!="null"){
						note=data.note;
					}
					var memo="";
					if(data.memo!=null && data.memo!="null"){
						memo=data.memo;
					}
					$("#note").text(note);
					$("#memo").text(memo);
					$("#fkSales_member").val(data.fkSales);
					refreshSelect();
					$("#fing1").val(data.fing1);
					$("#fing2").val(data.fing2);
					$("#ftype1_member").val(data.ftype1);
					$("#ftype2_member").val(data.ftype2);
					if (data.fing1 != null || data.fing2 != null) {
						$("#fingerImg_member").attr("src", base + "commonCss/images/zhiwen_c.png");
					} else {
						$("#fingerImg_member").attr("src", base + "commonCss/images/zhiwen_w.png");
					}

					$('#memberPop').window('open');
				}
				return;
			},
			error : function(XMLHttpRequest) {
				ajaxError(XMLHttpRequest);
			}
		});
	}
}

// 打开非会员
function openNotMember(memberId) {
	$("#imagePath_notMember").val();
	$("#fing1_notMember").val("");
	$("#fing2_notMember").val("");
	if (memberId == "") {
		$("#pkMemId").val("");
		$("#notMemberImage").attr("src", base + "commonCss/images/index_03.gif");
		$("#memName").val("");
		//$("#memType").val("2");
		$("#girlRadioButton").attr("checked", false);
		$("#boyRadioButton").attr("checked", false);
		$("#isVip").attr("checked", false);
		$("#birthday").val("");
		$("#country").val("");
		$("#pk").hide();
		$("#spanMemberId").html("");
		$("#canConsume").attr("checked", false);
		$("#pwd1").val("");
		$("#pwd2").val("");
		$("#signDisclaimer").attr("checked", false);
		$("#fkSales").val("");
		$("#fing1").val("");
		$("#fing2").val("");
		$("#ftype1_notMember").val("");
		$("#ftype2_notMember").val("");
		$("#fingerImg_notMember").attr("src", base + "commonCss/images/zhiwen_w.png");
		refreshSelect();
		$("#notMemberPop").attr("title", "新增");
	} else {
		$.ajax({
			cache : false,
			type : "get",
			dataType : "json",
			url : base + "filemanagement/queryMemberById.shtml",
			data : "memberId=" + memberId,
			success : function(data) {
				if (data == null) {
					alertInfo("没有匹配的会员!");
				} else {
					$("#pkMemId").val(data.pkMemId);
					if (data.imagePath == null) {
						$("#notMemberImage").attr("src", base + "commonCss/images/index_03.gif");
					} else {
						$("#imagePath_notMember").val(data.imagePath);
						$("#notMemberImage").attr("src",base + "common/showImage.shtml?imagePath=" + data.imagePath);
					}
					$("#memName").val(data.memName);
					$("#memType").val(data.memType);
					if (data.sex == "男") {
						$("#boyRadioButton").attr("checked", true);
					} else {
						$("#girlRadioButton").attr("checked", true);
					}
					if (data.isVip == "1") {
						$("#isVip").attr("checked", true);
					} else {
						$("#isVip").attr("checked", false);
					}
					$("#birthday").val(data.birthdayPage.replace(/\./g, "-"));
					$("#country").val(data.country);
					$("#pk").show();
					$("#spanMemberId").html(data.pkMemId);
					if (data.canConsume == "1") {
						$("#canConsume").attr("checked", true);
					} else {
						$("#canConsume").attr("checked", false);
					}
					$("#pwd1").val(data.receivePwd);
					$("#pwd2").val(data.receivePwd);
					if (data.signDisclaimer == "1") {
						$("#signDisclaimer").attr("checked", true);
					} else {
						$("#signDisclaimer").attr("checked", false);
					}
					$("#fkSales").val(data.fkSales);
					$("#fing1").val(data.fing1);
					$("#fing2").val(data.fing2);
					$("#ftype1_notMember").val(data.ftype1);
					$("#ftype2_notMember").val(data.ftype2);
					if (data.fing1 != null || data.fing2 != null) {
						$("#fingerImg_notMember").attr("src", base + "commonCss/images/zhiwen_c.png");
					} else {
						$("#fingerImg_notMember").attr("src", base + "commonCss/images/zhiwen_w.png");
					}
					refreshSelect();
				}
				return;
			},
			error : function(XMLHttpRequest) {
				ajaxError(XMLHttpRequest);
			}
		});
		$("#notMemberPop").attr("title", "编辑");
	}
	$('#notMemberPop').window('open');
}

// 打开新增/编辑监护人
function openGua(guaId) {
	if (familyId == "") {
		alertError("请先添加会员信息！");
		return false;
	}
	$("#imagePath_gua").val("");
	$("#fing1_gua").val("");
	$("#fing2_gua").val("");
	if (guaId == "") {
		$("#pkGuaId").val("");
		$("#imagePath_gua").val("");
		$("#guaImage").attr("src", base + "commonCss/images/index_02.gif");
		$("#fkReltypeId").val("");
		refreshSelect();
		$("#name").val("");
		$("#telNum").val("");
		$("#email").val("");
		$("#canConsume").attr("checked", false);
		$("#canReceive").attr("checked", false);
		$("#fing1").val("");
		$("#fing2").val("");
		$("#fingerImg_gua").attr("src", base + "commonCss/images/zhiwen_w.png");
		$("#guaPop").attr("title", "新增");
	} else {
		$.ajax({
			cache : false,
			type : "get",
			dataType : "json",
			url : base + "filemanagement/queryGuaById.shtml",
			data : "guaId=" + guaId,
			success : function(data) {
				if (data == null) {
					alertInfo("没有匹配的监护人!");
				} else {
					$("#pkGuaId").val(data.pkGuaId);
					if (data.imagePath == null) {
						$("#guaImage").attr("src", base + "commonCss/images/index_02.gif");
					} else {
						$("#imagePath_gua").val(data.imagePath);
						$("#guaImage").attr("src", base + "common/showImage.shtml?imagePath=" + data.imagePath);
					}
					$("#fkReltypeId").val(data.fkReltypeId);
					refreshSelect();
					$("#name").val(data.name);
					$("#telNum").val(data.telNum);
					$("#email").val(data.email);
					if (data.canConsume == "1") {
						$("#canConsume_gua").attr("checked", true);
					} else {
						$("#canConsume_gua").attr("checked", false);
					}
					if (data.canReceive == "1") {
						$("#canReceive").attr("checked", true);
					} else {
						$("#canReceive").attr("checked", false);
					}
					$("#fing1").val(data.fing1);
					$("#fing2").val(data.fing2);
					if (data.fing1 != null || data.fing2 != null) {
						$("#fingerImg_gua").attr("src", base + "commonCss/images/zhiwen_c.png");
					} else {
						$("#fingerImg_gua").attr("src", base + "commonCss/images/zhiwen_w.png");
					}
				}
				return;
			},
			error : function(XMLHttpRequest) {
				ajaxError(XMLHttpRequest);
			}
		});
		$("#guaPop").attr("title", "编辑");
	}
	$('#guaPop').window('open');
}

// 删除会员
function deleteMember(memberId,status) {
	if(status!=5){
		alertInfo("只能删除新建会员!");
		return;
	}
	
	/*var memberSize=$("#listTop li").size();
	if(memberSize<=1){
		alertInfo("不能删除同一家庭下的最后一个会员!");
		return;
	}*/
	
	$.messager.confirm('提示','确认要删除吗?',function(r) {
		if (r) {
			$.ajax({
				cache : false,
				type : "get",
				dataType : "json",
				url : base + "filemanagement/deleteMember.shtml",
				data : "memberId=" + memberId,
				success : function(data) {
					if (data == 1) {
						$("#li_member_" + memberId).remove();
					}
				},
				error : function(XMLHttpRequest) {
					ajaxError(XMLHttpRequest);
				}
			});
		}
	});
}

// 删除监护人
function deleteGua(guaId) {
	/*var guaSize=$("#list li").size();
	if(guaSize<=1){
		alertInfo("不能删除同一家庭下的最后一个家庭成员!");
		return;
	}*/
	$.messager.confirm('提示','确认要删除吗?',function(r) {
		if (r) {
			$.ajax({
				cache : false,
				type : "get",
				dataType : "json",
				url : base + "filemanagement/deleteGua.shtml",
				data : "guaId=" + guaId,
				success : function(data) {
					if (data == 1) {
						$("#li_gua_" + guaId).remove();
					}
				},
				error : function(XMLHttpRequest) {
					ajaxError(XMLHttpRequest);
				}
			});
		}
	});
}

//修改会员
function updateMember(status,op,type) {
	var title="";
	if(status=="4"){
		title="确定停卡吗?";
	}else if(status=="0"){
		title="确定设为黑名单吗?";
	}
	
	var reason="",suspendStart="",suspendEnd="",mngFile="";
	if(op=="4"){//停卡
		reason=$("#suspendReason").text();
		suspendStart=$("#suspendStart").val();
		suspendEnd=$("#suspendEnd").val();
		mngFile=$("#mngFile").val();
		
		//非空判断
		if((suspendStart==null || suspendStart=="") && (suspendEnd==null || suspendEnd=="")){
			alertInfo("请至少选择一个时间，谢谢!");
			return;
		}
		if(reason==null || reason==""){
			alertInfo("请简单描述停卡原因，谢谢!");
			return;
		}
	}else if(op=="0"){//黑名单
		reason=$("#blackReason").text();
		if(reason==null || reason==""){
			alertInfo("请简单描述设为黑名单的原因，谢谢!");
			return;
		}
	}
	
	$.messager.confirm('提示',title,function(r) {
		if (r) {
			$.post(base + "filemanagement/updateMember.shtml",
		    		{
				 		id:'query',
				 		reason: reason,
				 		memberId: selMemberId,
				 		status: status,
				 		suspendStart: suspendStart,
				 		suspendEnd: suspendEnd,
				 		mngFile: mngFile
		    		},
		    		function (result){
		    			if (result == 1) {
							//$("#li_member_" + memberId).remove();
		    				//alertInfo("操作成功");
							if(status=="4"){
								//$('#j').window('close');
								if(type=="1"){
									location.href=base + "filemanagement/listVip.shtml";
								}else if(type=="2"){
									location.href=base + "filemanagement/listViewSingelTicket.shtml";
								}else{
									location.href=base + "filemanagement/viewVistors.shtml";
								}
							}else if(status=="0"){
								//$('#f').window('close');
								if(type=="1"){
									location.href=base + "filemanagement/listVip.shtml";
								}else if(type=="2"){
									location.href=base + "filemanagement/listViewSingelTicket.shtml";
								}else{
									location.href=base + "filemanagement/viewVistors.shtml";
								}
							}
						}
		    		}
		    	);
		}
	});
}

//取消停卡
function cancelStopCard(status) {
	$.messager.confirm('提示',"确认取消停卡吗",function(r) {
		if (r) {
			$.post(base + "filemanagement/cancelStopCard.shtml",
		    		{
				 		id:'query',
				 		memberId: selMemberId,
				 		status: status
		    		},
		    		function (result){
		    			if (result == 1) {
		    				location.href=base + "filemanagement/listVip.shtml";
		    			}
		    		}
		    	);
		}
	});
}

//修改会员历史
function updateHisMember(memberId,type) {
	var path="";
	if(type=="1"){//合同路径
		path=$("#fkContract").val();
	}else{//独玩免责路径
		path=$("#fkPlayDiscla").val();
	}
	//非空判断
	/*if(path==null || path==""){
		alertInfo("请选择上传文件，谢谢!");
		return;
	}*/
	/*$.messager.confirm('提示','确定上传吗',function(r) {
		if (r) {*/
			$.ajax({
				cache : false,
				type : "get",
				dataType : "json",
				url : base + "filemanagement/updateHisMember.shtml",
				data : "path=" + path + "&memberId=" + memberId + "&type=" + type,
				success : function(data) {
					if (data == 1) {
						//$("#li_member_" + memberId).remove();
						//alertInfo("操作成功");
						if(type=="1"){//合同路径
							$('#t').window('close');
						}else{//独玩免责路径
							$('#g').window('close');
						}
					}
				},
				error : function(XMLHttpRequest) {
					ajaxError(XMLHttpRequest);
				}
			});
		/*}
	});*/
}

//获取余额
function getMemerInfo(type) {
	if(type=="2"){
		var flag = getBackOrder();
		if(flag=="false"){
			return;
		}
	}
	$.ajax({
		cache : false,
		type : "get",
		dataType : "json",
		url : base + "filemanagement/getMemerInfo.shtml",
		data : "memberId=" + selMemberId,
		success : function(member) {
			if (member == null) {
				$("#balance").text("0");
				$("#Drawback").text("0");
			} else {
				if(type=="1"){//充值
					$("#balance").text(member.balance==null?"0":member.balance);
					$('#r').window('open');
				}else{//退款
					$("#balanceDrawback").text(member.balance==null?"0":member.balance);
					$("#moneyDrawback").text(member.balance==null?"0":member.balance);
					$("#amountDrawback").text(member.balance==null?"0":member.balance);
					if(member.balance==null || member.balance<=0){
						$("#toReback").attr("disabled","disabled");
						$("#toReback").removeClass().addClass("btn_no");
					}else{
						$("#toReback").removeAttr("disabled");
						$("#toReback").removeClass().addClass("btn");
					}
					
					$("#fkOriorderId").val(member.fkOrderId);
					$('#e').window('open');
				}
			}
		},
		error : function(XMLHttpRequest) {
			ajaxError(XMLHttpRequest);
		}
	});
}

//获取余额(充值后)
function getMemerInfo2(type) {
	$.ajax({
		cache : false,
		type : "get",
		dataType : "json",
		url : base + "filemanagement/getMemerInfo.shtml",
		data : "memberId=" + selMemberId,
		success : function(member) {
			if (member != null) {
				$("#memBalance").text(member.balance);
			}
		},
		error : function(XMLHttpRequest) {
			ajaxError(XMLHttpRequest);
		}
	});
}

//充值/退款
function recharge(type) {
	var money="";
	var title="";
	var fkOriorderId="";
	if(type=="1"){//充值
		money=$("#totalMoney").text();
		title="确定充值吗";
	}else if(type=="2"){//退款
		fkOriorderId=$("#fkOriorderId").val();
		money=$("#amountDrawback").text();
		title="确定退款吗";
	}else{//退票
		money=$("#amountTicket").text();
		title="确定退票吗";
	}
	$.messager.confirm('提示',title,function(r) {
		if (r) {
			/*$.ajax({
				cache : false,
				type : "get",
				dataType : "json",
				url : base + "filemanagement/recharge.shtml",
				data : "money=" + money + "&memberId=" + memberId + "&type=" + type,
				success : function(orderId) {
					if (orderId!="") {
						alertInfo("操作成功");
						if(type=="1"){//充值
							$("#orderId").val(orderId);
							//$('#r').window('close');
						}else{//退款
							$("#orderId2").val(orderId);
							//$('#e').window('close');
						}
					}
				},
					error : function(XMLHttpRequest) {
						ajaxError(XMLHttpRequest);
					}
			});*/
			
			$.post(base + "filemanagement/recharge.shtml",
		    		{
				 		id:'query',
				 		money: money,
				 		memberId: selMemberId,
				 		type: type,
				 		fkOriorderId: fkOriorderId
		    		},
		    		function (orderId){
		    			if (orderId!="") {
		    				alertInfo("订单保存成功");
							if(type=="1"){//充值
								$("#orderId").val(orderId);
								//$('#r').window('close');
								$("#toRecharge").attr("disabled","disabled").removeClass().addClass("btn_no");
							}else if(type=="2"){//退款
								$("#orderId2").val(orderId);
								//$('#e').window('close');
								$("#toReback").attr("disabled","disabled").removeClass().addClass("btn_no");
							}else{
								$("#orderId3").val(orderId);
								$('#ee').window('close');
								$("#toReTicket").attr("disabled","disabled").removeClass().addClass("btn_no");
							}
						}
		    		}
		    	);
		}
	});
}

//刷新付款结果
function refreshPayResult(type) {
	var orderId="";
	if(type=="1"){
		orderId=$("#orderId").val();
	}else if(type=="2"){
		orderId=$("#orderId2").val();
	}else if(type=="3"){
		orderId=$("#orderId4").val();
	}else{
		orderId=$("#orderId3").val();//退票
	}
	$.ajax({
		cache : false,
		type : "post",
		url : base + "membership/single/refreshPayResult.shtml",
		data : "orderId=" + orderId,
		success : function(data) {
			if (data == 1) {
				if(type=="1"){
					//$("#payId").val($("#orderId").val());
					$("#payId").val("已付款");
				}else if(type=="2"){
					//$("#payId2").val($("#orderId2").val());
					$("#payId2").val("已退款");
				}else if(type=="3"){
					//$("#payId2").val($("#orderId2").val());
					$("#payId3").val("已付款");
				}else{
					$("#payId4").val("已退票");
				}
			} else {
				if(type=="1"){
					$("#payId").val("未付款");
				}else if(type=="2"){
					$("#payId2").val("未退款");
				}else if(type=="3"){
					$("#payId3").val("未付款");
				}else{
					$("#payId4").val("未退票");
				}
			}
		},
		error : function() {
			alertError("查询失败!");
		}
	});
}

function openCameraPage(flag){
	var returnValue=window.showModalDialog(base + "filemanagement/openCamera.shtml",window,"height=500,width=611,scrollbars=yes,status =yes");
	if (returnValue==1) {
	        //发送ajax请求获取上传成功的图片名称
		$.ajax({
		        type: 'get',  
		        url: base + "filemanagement/getUploadedImageName.shtml",
		        dataType:"json",
		        cache: false,
		        async: false,
		        success: function(data) {
		        	var imageName=data.imageName;
		        	var imageSrc=base + "common/showImage.shtml?imagePath="+imageName;
		        	switch (flag) {
					case 1:
						//创建会员
						$("#memberImage").attr("src",imageSrc);
						$("#imagePath_member").val(imageName);
						break;
					case 2:
						//编辑会员
						$("#notMemberImage").attr("src",imageSrc);
						$("#imagePath_notMember").val(imageName);
						break;
					case 3:
						//编辑监护人
						$("#guaImage").attr("src",imageSrc);
						$("#imagePath_gua").val(imageName);
						break;
		        	}
	    	        },
					error : function(XMLHttpRequest) {
						ajaxError(XMLHttpRequest);
					}
		});  
		        
	}
}

// 打开指纹扫描界面
function openFingerPrint() {
	if ($("#fing1").val() == "") {
		$("#imgFing1").attr("src", base + "commonCss/images/zhiwen_w.png");
	} else {
		$("#imgFing1").attr("src", base + "commonCss/images/zhiwen_c.png");
	}
	if ($("#fing2").val() == "") {
		$("#imgFing2").attr("src", base + "commonCss/images/zhiwen_w.png");
	} else {
		$("#imgFing2").attr("src", base + "commonCss/images/zhiwen_c.png");
	}
	$('#fingerprintPop').window('open');
}

// 指纹类型 1：会员 2：非会员 3：监护人
var fingerType;
var fingerFlag;

/**
 * 采集指纹特征
 * @param flag
 * 	1 ：主手指
 * 	2 ：副手指
 */
function collectFingerFea(flag) {
	
	fingerData = null;
	fingerFlag = null;
	
	try {
		var data = collectFingerTemplate();
		if (data != "") {
			var exist = false;
			$.ajax({
				cache : false,
				async: false,
				type : "post",
				dataType : "json",
				url : base + "filemanagement/checkFingerprintExist.shtml",
				data : "fingerprint=" + encodeURIComponent(data),
				success : function(data) {
					if (data == 0) {
						alertError("指纹已存在于其它用户，不可重复创建！");
						exist = true;
						return;
					}
				},
				error : function(XMLHttpRequest) {
					ajaxError(XMLHttpRequest);
				}
			});
			if (exist) return;
			
			$("#fing" + flag).val(data);
			$("#imgFing" + flag).attr("src", base + "commonCss/images/zhiwen_c.png");
			fingerFlag = flag;
			$('#fingerPop').window('open');
		}
	} catch (e) {
		return "";
	}
}

function fingerprintOK() {
	if ($("#ftype1").val() != "" || $("#ftype2").val() != "") {
		$("#fingerImg_" + fingerType).attr("src", base + "commonCss/images/zhiwen_c.png");
	}
	$('#fingerprintPop').window('close');
}

function fingerSelectOK() {
	if ($("input[name='fingerIndex']:checked").val() == null) {
		alertError("请选择手指！");
		return false;
	}
	$("#ftype" + fingerFlag).val($("input[name='fingerIndex']:checked").val());
	$('#fingerPop').window('close');
}

function fingerSelectCancel() {
	$("#fing" + fingerFlag).val("");
	$("#ftype" + fingerFlag).val("");
	$("#fingerImg_" + fingerType).attr("src", base + "commonCss/images/zhiwen_w.png");
	$('#fingerPop').window('close');
}

//获取单次票集合
function getSingleTicket() {
	$.ajax({
		cache : false,
		type : "get",
		dataType : "json",
		url : base + "filemanagement/findSingleTicketByMemId.shtml",
		data : "pkMemId=" + selMemberId,
		success : function(singleTickets) {
			$("#listSingleTicket li").remove();
			if (singleTickets == null || singleTickets.length == 0) {
				//alertInfo("没有匹配的记录！");
			} else {
				trimJsonArray(singleTickets);
				$("#liCount").val(singleTickets.length);
				for (var i = 0; i < singleTickets.length; i++) {
					var prd = singleTickets[i];
					
					//给订单号赋值
					var orderId = prd.orderId;
					if(orderId!=null && orderId!=""){
						$("#fkOriorderId").val(orderId);
					}
					
					var prdName="";
					if(prd.prdName!=null){
						prdName=prd.prdName;
					}
					//var html="<li><label style='z-index: 10;' onclick='getAllTicketPrice("+i+");'><input id='prdPrice"+i+"' type='checkbox' name='prdPrice"+i+"' class='mr5' value='"+prd.productPrice+"'><input type='hidden' name='prdId"+i+"' value='"+prd.pkPrdId+"'>"+prdName+"</label></li>";
					var html="<li><label><input id='prdPrice"+i+"' type='checkbox' name='prdPrice"+i+"' class='mr5' value='"+prd.productPrice+"' onclick='getAllTicketPrice(this);'><input type='hidden' name='prdId"+i+"' value='"+prd.pkPrdId+"'>"+prdName+"</label></li>";
					$("#listSingleTicket").append(html);
				}
			}
			$('#ee').window('open');
		},
		error : function(XMLHttpRequest) {
			ajaxError(XMLHttpRequest);
		}
	});
}

//获取课程集合
function getAllLessons() {
	$.ajax({
		cache : false,
		type : "get",
		dataType : "json",
		url : base + "filemanagement/findAllLessonsProduct.shtml",
		success : function(lessonsProducts) {
			$("#listTbodyLessons tr").remove();
			if (lessonsProducts == null || lessonsProducts.length == 0) {
				//alertInfo("没有匹配的记录！");
			} else {
				trimJsonArray(lessonsProducts);
				$("#tableCount").val(lessonsProducts.length);
				for (var i = 0; i < lessonsProducts.length; i++) {
					var prd = lessonsProducts[i];
					var prdName="";
					if(prd.prdName!=null){
						prdName=prd.prdName;
					}
					var productPrice="0.00";
					if(prd.productPrice!=null && prd.productPrice!=""){
						productPrice=prd.productPrice;
					}
					var startTime="";
					if(prd.startTime!=null){
						startTime=prd.startTime.substring(0,10);
					}
					var endTime="";
					if(prd.endTime!=null){
						endTime=prd.endTime.substring(0,10);
					}
					var cnt="0";
					if(prd.cnt!=null && prd.cnt!=""){
						cnt=prd.cnt;
					}
					var html="<tr>";
					html+="<td><input type='checkbox' name='prdPrice"+i+"' value='"+prd.productPrice+"' onclick=isEnable(this,'"+i+"')><input type='hidden' name='prdId"+i+"' value='"+prd.pkPrdId+"'><input type='hidden' name='startTime"+i+"' value='"+startTime+"'><input type='hidden' name='endTime"+i+"' value='"+endTime+"'></td>"+
					  "<td>"+prd.pkPrdId+"</td>"+
					  "<td>"+prdName+"</td>"+
					  "<td>"+cnt+"</td>"+
					  "<td>"+startTime+"-"+endTime+"</td>"+
					  "<td><a href=javascript:jian('"+i+"','"+prd.pkPrdId+"','"+productPrice+"') class='jia_icon mr5' disabled='disabled' id='jian"+i+"'></a><input type='text' name='prdCount"+i+"' class='iput_t w50' style='text-align: center;' value='0' id='prdCount"+i+"' disabled='disabled' readonly='readonly'><a href=javascript:jia('"+i+"','"+prd.pkPrdId+"','"+productPrice+"') class='jian_icon ml5' disabled='disabled' id='jia"+i+"'></a></td>"+
					  "<td>"+productPrice.toFixed(2)+"</td>"+
					  "<td id='totalCost"+i+"'>0.00</td>"+
					  "</tr>";
					  $("#listTbodyLessons").append(html);
				}
			}
			$('#ex').window('open');
			$("#buyCourse").attr("disabled","disabled").removeClass().addClass("btn_no");
		},
		error : function(XMLHttpRequest) {
			ajaxError(XMLHttpRequest);
		}
	});
	
	clearup("gmkc");//初始化
}

function isEnable(t,count){
	if(t.checked==true){
		$("#prdCount" + count).removeAttr("disabled");
		$("#jian" + count).removeAttr("disabled");
		$("#jia" + count).removeAttr("disabled");
	}else{
		$("#prdCount" + count).attr("disabled","disabled");
		$("#jian" + count).attr("disabled","disabled");
		$("#jia" + count).attr("disabled","disabled");
		
		var totalCosts = $("#totalCost" + count).text();//小计
		var totalAmounts = $("#totalAmount").text();//总价
		var infactAmounts = $("#infactAmount").text();//应付金额
		
		//数量清0
		$("#prdCount" + count).val("0");
		
		//总价减去禁用的小计
		$("#totalAmount").text((parseFloat(totalAmounts) - parseFloat(totalCosts)).toFixed(2));
		
		//应付金额减去禁用的小计
		$("#infactAmount").text((parseFloat(infactAmounts) - parseFloat(totalCosts)).toFixed(2));
		
		//小计清0.00
		$("#totalCost" + count).text("0.00");
	}
}

//购买课程
function buyCourse() {
	//判断是否购买了商品
	var totalPrdCount = $("#totalPrdCount").val();
	if(parseInt(totalPrdCount)<=0){
		alertInfo("您还未选择课程!");
		return;
	}
	
	//把text值付给文本域提交
	var infactAmount=$("#infactAmount").text();
	$("#infactAmountInput").val(infactAmount);
	$.messager.confirm('提示','确定购买吗',function(r) {
		if (r) {
			$.ajax({
				cache : false,
				type : "post",
				url : base + "filemanagement/buyCourse.shtml",
				data :$("#buyCourseForm").serialize(),
				success : function(orderId) {
					if (orderId!="") {
	    				alertInfo("订单保存成功");
	    				$("#orderId4").val(orderId);
	    				//getAllLessons();
	    				$("#buyCourse").attr("disabled","disabled").removeClass().addClass("btn_no");//禁用按钮
					}
				},
				error : function(XMLHttpRequest) {
					ajaxError(XMLHttpRequest);
				}
			});
		}
	});
}

//退票
function refundGoods() {
	//把text值付给文本域提交
	var amountTicket=$("#amountTicket").text();
	$("#amountTicketInput").val(amountTicket);
	$.messager.confirm('提示','确定退票吗',function(r) {
		if (r) {
			$.ajax({
				cache : false,
				type : "post",
				url : base + "filemanagement/refundGoods.shtml",
				data :$("#returnGoodForm").serialize(),
				success : function(orderId) {
					if (orderId!="") {
	    				alertInfo("订单保存成功");
	    				$("#orderId3").val(orderId);
	    				$("#toReTicket").attr("disabled","disabled").removeClass().addClass("btn_no");//禁用按钮
					}
				},
				error : function(XMLHttpRequest) {
					ajaxError(XMLHttpRequest);
				}
			});
		}
	});
}

function getAllTicketPrice(t){
	//var t=$("#prdPrice"+row)[0];//加上索引转为dom对象
	var producPrice=t.value;//alert(producPrice);
	if(producPrice==null || producPrice==""){
		producPrice=0;
	}
	var moneyTicketBak=$("#moneyTicketBak").val();
	if(moneyTicketBak==null || moneyTicketBak==""){
		moneyTicketBak=0;
	}
	var moneyTicket=$("#moneyTicket").val();
	if(moneyTicket==null || moneyTicket==""){
		moneyTicket=0;
	}
	var amountTicket=$("#amountTicket").text();
	if(amountTicket==null || amountTicket==""){
		amountTicket=0;
	}
	if(t.checked==true){
		moneyTicketBak=parseFloat(moneyTicketBak)+parseFloat(producPrice);
		moneyTicket=parseFloat(moneyTicket)+parseFloat(producPrice);
		amountTicket=parseFloat(amountTicket)+parseFloat(producPrice);
	}else{
		moneyTicketBak=parseFloat(moneyTicketBak)-parseFloat(producPrice);
		moneyTicket=parseFloat(moneyTicket)-parseFloat(producPrice);
		amountTicket=parseFloat(amountTicket)-parseFloat(producPrice);
	}
	$("#moneyTicketBak").val(moneyTicketBak);
	$("#moneyTicket").val(moneyTicket);
	$("#amountTicket").text(amountTicket);
	
	//判断按钮是否可用
	if(parseFloat(amountTicket)>0){
		$("#toReTicket").removeAttr("disabled");
		$("#toReTicket").removeClass().addClass("btn");
	}else{
		$("#toReTicket").attr("disabled","disabled");
		$("#toReTicket").removeClass().addClass("btn_no");
	}
}

//获取文件路径
function getFilePath(type){
	$.ajax({
		cache : false,
		type : "post",
		url : base + "filemanagement/getFilePath.shtml",
		data :{
			memberId: selMemberId,
			type:type
		},
		success : function(filePath) {
			if(type=="1"){
				$("#fkContract").val("");
				//合同
				if (filePath!="") {
					$("#scAccount").show();
					//$("#contractName").attr("href",filePath);
					$("#contractName").text(filePath.substring(filePath.lastIndexOf("\\")+1,filePath.length));
					$("#attFile1").val(filePath.substring(filePath.lastIndexOf("\\")+1,filePath.length));
				}else{
					$("#scAccount").hide();
				}
				clearup('ht');
				$('#t').window('open');
			}else if(type=="2"){
				$("#fkPlayDiscla").val("");
				//独玩
				if (filePath!="") {
					$("#scSelf").show();
					//$("#selfFilePath").attr("href",filePath);
					$("#disclaName").text(filePath.substring(filePath.lastIndexOf("\\")+1,filePath.length));
					$("#attFile2").val(filePath.substring(filePath.lastIndexOf("\\")+1,filePath.length));
				}else{
					$("#scSelf").hide();
				}
				clearup('dw');
				$('#g').window('open');
			}else{
				$("#mngFile").val("");
				//停卡
				if (filePath!="") {
					$("#stopCard").show();
					//$("#stopCardFilePath").attr("href",filePath);
					$("#stopcardName").text(filePath.substring(filePath.lastIndexOf("\\")+1,filePath.length));
					$("#attFile3").val(filePath.substring(filePath.lastIndexOf("\\")+1,filePath.length));
				}else{
					$("#stopCard").hide();
				}
				clearup('tk');
				$('#j').window('open');
			}
		},
		error : function(XMLHttpRequest) {
			ajaxError(XMLHttpRequest);
		}
	});
}

//清除保存值
function clearup(op){
	if(op=="hmd"){
		$('#blackReason').text('');
	}else if(op=="tk"){
		$('#suspendStart').val('');
		$('#suspendEnd').val('');
		$('#suspendReason').text('');
		$('#mngFile').val('');
	}else if(op=="dw"){
		$('#f_file2').val('');
	}else if(op=="ht"){
		$('#f_file').val('');
	}else if(op=="tk2"){
		$('#moneyDrawback').text('');
		$('#amountDrawback').text('0');
		$('#payId2').val('');
		//$("#toReback").removeAttr("disabled").removeClass().addClass("btn");//复用退款按钮
	}else if(op=="cz"){
		$('#money').val('');
		$('#totalMoney').text('0');
		$('#payId').val('');
		//$("#toRecharge").removeAttr("disabled").removeClass().addClass("btn");//复用充值按钮
	}else if(op=="tp"){
		$("#moneyTicketBak").val('0');
		$("#moneyTicket").val('0');
		$("#amountTicket").text('0');
		//$("#toReTicket").removeAttr("disabled").removeClass().addClass("btn");//复用退票按钮
	}else if(op=="gmkc"){
		$('#payId3').val('');
		
		//总价清0
		$("#totalAmount").text("0.00");
		
		//应付金额清0
		$("#infactAmount").text("0.00");
		
		//小计清0
		var courseCount=$("#listTbodyLessons tr").size();
		if(courseCount>0){
			for (var i = 0; i < courseCount; i++) {
				$("#totalCost" + i).text("0.00");
			}
		}
		$("#buyCourse").removeAttr("disabled").removeClass().addClass("btn");//复用购买课程按钮
	}
}
